package com.hellogood.yaohuo.server.db;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import java.util.Vector;

import org.apache.log4j.Logger;

public class ConnectionPool {

	Logger log = Logger.getLogger(this.getClass());
	private Vector<Connection> pool;
	private final String driverClassName = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
	private String url = "jy.sjwy.icevpn.org";
	private String hostName = "jy.sjwy.icevpn.org";
	private String dbName = "yh";
	private String username = "sa";
	private String password = "mjwdlsd@163.com";
	public static String hxDbName = "cssysdb";
	private int poolSize = 30;
	
	private static ConnectionPool instance = null;

	private ConnectionPool() {	
		init();		
	}

	private void init() {
		pool = new Vector<Connection>(poolSize);
		// readConfig();
		url = "JDBC:microsoft:SQLServer://" + hostName + ":1433; DatabaseName=" + dbName + ";SelectMethod=cursor;";
		addConnection();
	}
	
	private void parseConfig(){
		Properties prop = new Properties();		
		try {
			URL url = this.getClass().getResource("config.properties");
			prop.load(url.openStream());
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		hostName = prop.getProperty("host");
		username = prop.getProperty("username");
		password = prop.getProperty("password");
		dbName = prop.getProperty("dbname");
		hxDbName = prop.getProperty("hxdbname");
	}

	public synchronized void release(Connection conn) {
		pool.add(conn);
	}

	public synchronized void closePool() {
		for (int i = 0; i < pool.size(); i++) {
			try {
				pool.get(i).close();
			} catch (SQLException e) {
				e.printStackTrace();
				log.error(e.getMessage());
			}
			pool.remove(i);
		}
	}

	public static ConnectionPool getInstance() {
		if (instance == null) {
			instance = new ConnectionPool();
		}
		return instance;
	}

	public synchronized Connection getConnection() {
		if (pool.size() > 0) {
			Connection conn = pool.get(0);
			pool.remove(conn);
			return conn;
		} else {
			return null;
		}
	}

	private void addConnection() {
		Connection conn = null;
		for (int i = 0; i < poolSize; i++) {
			try {
				Class.forName(driverClassName);
				conn = DriverManager.getConnection(url, username, password);
				conn.setCatalog(dbName);
				pool.add(conn);
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
				log.error(e.getMessage());
			} catch (SQLException e) {
				e.printStackTrace();
				log.error(e.getMessage());
				log.error(e.getStackTrace());
			}
		}
	}

	/*
	 * private void readConfig() { this.hostName = "127.0.0.1"; this.dbName =
	 * "yh"; this.username = "sa"; this.password = ""; //
	 * '220.166.63.14,1433';'sa';'hgdzabcd' // this.hostName = "220.166.63.14";
	 * // this.dbName = "sysdb"; // this.username = "sa"; // this.password =
	 * "hgdzabcd";
	 * 
	 * this.url = "JDBC:microsoft:SQLServer://" + hostName +
	 * ":1433; DatabaseName=" + dbName + ";SelectMethod=cursor;"; /* XMLHandler
	 * handler = new XMLHandler(); try { String[] config =
	 * handler.parseConfig(); this.hostName = config[0]; this.dbName =
	 * config[1]; this.username = config[2]; this.password = config[3]; this.url
	 * = "JDBC:microsoft:SQLServer://" + hostName + ":1433; DatabaseName=" +
	 * dbName + ";SelectMethod=cursor;"; } catch (IOException e) {
	 * e.printStackTrace(); log.error(e.getMessage());
	 * JOptionPane.showMessageDialog(null, "请检查配置文件config.xml是否存在"); }
	 */
	// } */
}
